package jmine.tec.web.wicket.component.table;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import jmine.tec.report.api.Report;
import jmine.tec.web.wicket.component.command.TableCommandsProvider;
import jmine.tec.web.wicket.component.command.button.SearchCommandHandler;
import jmine.tec.web.wicket.pages.form.ListPage;

import org.apache.wicket.markup.html.WebMarkupContainer;

import bancosys.tec.persist.Persistable;

/**
 * Painel que renderiza uma tabela de resultado de pesquisa com checkbox para selecionar as linhas na primeira coluna.
 * 
 * @author Rafael Volpato
 * @date Sep 28, 2011
 * @param <BO> Tipo da entidade da tabela
 */
public class SearchResultAjaxSelectionTable<BO extends Persistable> extends SearchResultTable<BO> {

    private final Set<Serializable> chosen;

    /**
     * Construtor.
     * 
     * @param id id
     * @param report {@link Report}
     * @param listPage {@link ListPage}
     * @param <M> tipo do model do {@link ListPage}
     */
    public <M> SearchResultAjaxSelectionTable(String id, Report report, ListPage<M, BO> listPage) {
        this(id, report, listPage.getEntityClass(), listPage, listPage, new HashSet<Serializable>());
    }

    /**
     * Construtor.
     * 
     * @param id id
     * @param report {@link Report}
     * @param targetClass targetClass
     * @param searchHandler {@link SearchCommandHandler}
     * @param commandsProvider {@link TableCommandsProvider}
     * @param chosen armazena os elementos que foram selecionados
     */
    public SearchResultAjaxSelectionTable(String id, Report report, Class<BO> targetClass, SearchCommandHandler<BO> searchHandler,
            TableCommandsProvider commandsProvider, Set<Serializable> chosen) {
        super(id);
        this.chosen = chosen;
        SearchResultAjaxSelectionTableBuilder<BO> builder =
                new SearchResultAjaxSelectionTableBuilder<BO>(targetClass, searchHandler, commandsProvider, chosen);
        WebMarkupContainer createInlineMarkup = builder.createInlineMarkup(REPORT_TABLE_COMPONENT_ID, report);
        this.addOrReplace(createInlineMarkup);
    }

    /**
     * The chosen set.
     * 
     * @return {@link Set}
     */
    public Set<Serializable> getChosen() {
        return this.chosen;
    }
}
